Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

middleware-flow

Package Overview
Dependencies
Maintainers
3
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

middleware-flow

Middleware control flow library: series, parallel, or, and

  • 0.5.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
3
Created
Source

middleware-flow Build Status

Middleware control flow library

Installation

npm install middleware-flow

Examples

series(middlewares...)

var series = require('middleware-flow').series;
var app = require('express')();

app.use(series(mw1, mw2, mw2)); // equivalent to app.use(mw1, mw2, mw3);

parallel(middlewares...)

var parallel = require('middleware-flow').parallel;
var app = require('express')();
                                  // runs the middlewares in 'parallel'
app.use(parallel(mw1, mw2, mw2)); // if err, returns the first error that occurred

or(middlewares...)

var or = require('middleware-flow').or;
var app = require('express')();
                                             // runs the middlewares in series, until one passes (no next(err));
app.use(or(user.isOwner, user.isModerator)); // if err, returns the first error that occurred

and(middlewares...)

Same as series.

if(value).then(middlewares...).else(middlewares...)

var if = require('middleware-flow').if;
var app = require('express')();

app.use(
  if(true)
    .then(one, two, three)
    .else(error)
);

syncIf(fn).then(middlewares...).else(middlewares...)

var syncIf = require('middleware-flow').syncIf;
var app = require('express')();

app.use(
  syncIf(nameQueryExists)   // accepts a sync function that returns a boolean
    .then(one, two, three)  // true -> then, error -> skips all next(err)
    .else(error)
);
function nameQueryExists (req, res) {
  return exists(req.query.name);
}
function exists (val) {
  return val !== null && val !== undefined;
}

asyncIf(fn).then(middlewares...).else(middlewares...)

var asyncIf = require('middleware-flow').asyncIf;
var or = require('middleware-flow').or;
var fs = require('fs');
var app = require('express')();

app.use(
  asyncIf(bodyFileExists)    // expects boolean as the result argument
    .then(one, two, three)   // true -> then, false -> else, error -> skips all next(err)
    .else(other)
);
function logExists (req, res, cb) {
  fs.exists(req.body.file, function (exists) {
    cb(null, exists);
  });
}

mwIf(middleware).then(middlewares..).else(middlewares..)

var mwIf = require('middleware-flow').mwIf;
var app = require('express')();

app.use(
  mwIf(userIsModerator)    // error here, just runs the else middlewares
    .then(one, two, three) // no error -> then, error -> else
    .else(other)           // if other is an error middleware it will recieve
                           //   the error else the error will be ignored
);
function userIsModerator (req, res, next) {
  if (!req.user.isModerator) {
    next(new Error('access denied'));
  }
  else {
    next();
  }
}

try(middlewares..).catch(middlewares..)

var flow = require('middleware-flow');
var app = require('express')();

app.use(
  flow.try(saveUser) // error here, just runs the catch middlewares
    .catch(rollback) // no error -> other, error -> rollback
                     // if rollback is an error middleware it will recieve
                     //   the error else the error will be ignored
);
function saveUser (req, res, next) {
  db.save(req.user, next);
}

License

MIT

FAQs

Package last updated on 15 Jul 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc